package org.jeecg.glr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.glr.entity.GlrRadio;
import org.jeecg.glr.entity.GlrRadioServer;
import org.jeecg.glr.entity.GlrRadioTaskMember;
import org.jeecg.glr.entity.GlrResource;
import org.jeecg.glr.service.IGlrRadioServerService;
import org.jeecg.glr.service.IGlrRadioService;
import org.jeecg.glr.service.IGlrRadioTaskMemberService;
import org.jeecg.glr.service.IGlrResourceService;
import org.jeecg.glr.util.GLRConstant;
import org.jeecg.glr.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 任务关联信息
 * @Author: jeecg-boot
 * @Date: 2020-02-14
 * @Version: V1.0
 */
@RestController
@RequestMapping("/glr/glrRadioTaskMember")
@Slf4j
public class GlrRadioTaskMemberController extends JeecgController<GlrRadioTaskMember, IGlrRadioTaskMemberService> {
    @Autowired
    private IGlrRadioTaskMemberService glrRadioTaskMemberService;
    @Autowired
    private IGlrRadioService glrRadioService;
    @Autowired
    private IGlrResourceService glrResourceService;

    @Autowired
    private IGlrRadioServerService glrRadioServerService;

    /**
     * 分页列表查询
     *
     * @param glrRadioTaskMember
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/radioList")
    public Result<?> radioList(GlrRadioTaskMember glrRadioTaskMember,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req)  throws Exception{
        QueryWrapper<GlrRadioTaskMember> queryWrapper = QueryGenerator.initQueryWrapperCode(glrRadioTaskMember, req.getParameterMap());
        Page<GlrRadioTaskMember> page = new Page<GlrRadioTaskMember>(pageNo, pageSize);
        IPage<GlrRadioTaskMember> pageList = glrRadioTaskMemberService.page(page, queryWrapper);
        List<GlrRadioTaskMember> list = pageList.getRecords();

        GlrRadioServer glrRadioServer=glrRadioServerService.doGetByPropertyName("code", GLRConstant.SERVER_CODE_RATTOP);
        String stJSessionID = glrRadioServerService.getRattopSessionId(glrRadioServer);

        for (GlrRadioTaskMember m : list) {
            GlrRadio domain= glrRadioService.getById(m.getMemberId());
            glrRadioServerService.updateGlrRadioStatus(domain,glrRadioServer,stJSessionID);
            m.setGlrRadio(domain);
            m.setCompanyCode(domain.getCompanyCode());
        }
        return Result.ok(pageList);
    }

    @GetMapping(value = "/resourceList")
    public Result<?> resourceList(GlrRadioTaskMember glrRadioTaskMember,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<GlrRadioTaskMember> queryWrapper = QueryGenerator.initQueryWrapperCode(glrRadioTaskMember, req.getParameterMap());
        Page<GlrRadioTaskMember> page = new Page<GlrRadioTaskMember>(pageNo, pageSize);
        IPage<GlrRadioTaskMember> pageList = glrRadioTaskMemberService.page(page, queryWrapper);
        List<GlrRadioTaskMember> list = pageList.getRecords();
        for (GlrRadioTaskMember m : list) {
            GlrResource glrResource=glrResourceService.getById(m.getMemberId());
            if (glrResource!=null){
                m.setGlrResource(glrResource);
                m.setCompanyCode(m.getGlrResource().getCompanyCode());
            }
        }
        return Result.ok(pageList);
    }
    /**
     * 添加
     *
     * @param glrRadioTaskMember
     * @return
     */
    @AutoLog(value = "添加音柱任务成员",logType= CommonConstant.LOG_TYPE_2)
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody GlrRadioTaskMember glrRadioTaskMember) {
        glrRadioTaskMemberService.save(glrRadioTaskMember);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param glrRadioTaskMember
     * @return
     */
    @AutoLog(value = "修改音柱任务成员",logType= CommonConstant.LOG_TYPE_2)
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody GlrRadioTaskMember glrRadioTaskMember) {
        glrRadioTaskMemberService.updateById(glrRadioTaskMember);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "删除音柱任务成员",logType= CommonConstant.LOG_TYPE_2)
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        glrRadioTaskMemberService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "批量删除音柱任务成员",logType= CommonConstant.LOG_TYPE_2)
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.glrRadioTaskMemberService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        GlrRadioTaskMember glrRadioTaskMember = glrRadioTaskMemberService.getById(id);
        if (glrRadioTaskMember == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(glrRadioTaskMember);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param glrRadioTaskMember
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GlrRadioTaskMember glrRadioTaskMember) {
        return super.exportXls(request, glrRadioTaskMember, GlrRadioTaskMember.class, "任务关联信息");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, GlrRadioTaskMember.class);
    }
    /**
     * 音柱任务绑定资源
     *
     * @return
     */
    @AutoLog(value = "音柱任务绑定资源",logType= CommonConstant.LOG_TYPE_2)
    @PostMapping(value = "/bingResource")
    public Result<?> bingResource(@RequestBody GlrRadioTaskMember glrRadioTaskMember) {
        glrRadioTaskMemberService.bingResource(glrRadioTaskMember);
        return Result.ok();
    }
    /**
     * 音柱任务绑定音柱
     *
     * @return
     */
    @AutoLog(value = "音柱任务绑定音柱",logType= CommonConstant.LOG_TYPE_2)
    @PostMapping(value = "/bingRadio")
    public Result<?> bingRadio(@RequestBody GlrRadioTaskMember glrRadioTaskMember) {
        glrRadioTaskMemberService.bingRadio(glrRadioTaskMember);
        return Result.ok();
    }
    /**
     * 分页列表查询
     *
     * @param glrRadio
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/TaskRadiolist")
    public Result<?> TaskRadiolist(GlrRadio glrRadio,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) throws Exception {
        QueryWrapper<GlrRadio> queryWrapper = QueryGenerator.initQueryWrapperCode(glrRadio, req.getParameterMap());
        String stCompanyCode=req.getParameter("companyCode");//公司代码

        List<String> strList = glrRadioTaskMemberService.getRadioByTaskId(req.getParameter("taskId"));
        if(strList!=null&&strList.size()>0){
            queryWrapper.notIn("id", strList);
        }
        if (StringUtil.isNotEmpty(stCompanyCode)){
            queryWrapper.eq("company_code",stCompanyCode);
        }
        Page<GlrRadio> page = new Page<GlrRadio>(pageNo, pageSize);
        IPage<GlrRadio> pageList = glrRadioService.page(page, queryWrapper);
        List<GlrRadio> list=pageList.getRecords();

        GlrRadioServer glrRadioServer=glrRadioServerService.doGetByPropertyName("code", GLRConstant.SERVER_CODE_RATTOP);
        String stJSessionID = glrRadioServerService.getRattopSessionId(glrRadioServer);

        for (GlrRadio domain: list) {
            glrRadioServerService.updateGlrRadioStatus(domain,glrRadioServer,stJSessionID);
        }
        return Result.ok(pageList);
    }

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/taskResourceList")
    public Result<?> taskResourceList(GlrResource glrResource,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<GlrResource> queryWrapper = QueryGenerator.initQueryWrapperCode(glrResource, req.getParameterMap());
        String stTaskId=req.getParameter("taskId");
        String memberType=req.getParameter("memberType");
        String companyCode=req.getParameter("companyCode");
        List<String> strList = glrRadioTaskMemberService.getResourceByTaskId(stTaskId);
        if(strList!=null&&strList.size()>0){
            queryWrapper.notIn("id", strList);
        }
        queryWrapper.eq("company_code", companyCode);
        queryWrapper.eq("equipment_Type", GLRConstant.GLR_RESOURCE_TYPE_1);
        Page<GlrResource> page = new Page<GlrResource>(pageNo, pageSize);
        IPage<GlrResource> pageList = glrResourceService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

}
